package com.gbq.qimo.servlet;

import com.gbq.qimo.pojo.Admin;
import com.gbq.qimo.pojo.Student;
import com.gbq.qimo.pojo.Teacher;
import com.gbq.qimo.service.AdminService;
import com.gbq.qimo.service.StudentService;
import com.gbq.qimo.service.TeacherService;
import com.gbq.qimo.service.impl.AdminServiceImpl;
import com.gbq.qimo.service.impl.StudentServiceImpl;
import com.gbq.qimo.service.impl.TeacherServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @ClassName RegisterServlet
 * @Description TODO
 * @Author guobenqi
 * @Date 2021/6/5 18:33
 */
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String verifycode = request.getParameter("verifycode");
        //校验验证码
        String checkcode_server = (String)request.getSession().getAttribute("CHECKCODE_SERVER");
        request.getSession().removeAttribute("CHECKCODE_SERVER");
        if (!checkcode_server.equals(verifycode)){
            request.setAttribute("register_msg","验证码错误！");
            request.getRequestDispatcher("/register.jsp").forward(request,response);
            return;
        }
        Map<String, String[]> map = request.getParameterMap();

        if (map.get("loginType") == null){
            request.setAttribute("register_msg","请勾选自己的角色!");
            request.getRequestDispatcher("/register.jsp").forward(request,response);
            return;
        }

        if (!map.get("password")[0].equals(map.get("password2")[0])){
            request.setAttribute("register_msg","两次密码输入不一样!");
            request.getRequestDispatcher("/register.jsp").forward(request,response);
            return;
        }


        //管理员登录\
        if (map.get("loginType")[0].equals("teacher")){
            Teacher teacher = new Teacher();
            try{
                BeanUtils.populate(teacher,map);
            }catch (IllegalAccessException e){
                e.printStackTrace();
            }catch (InvocationTargetException e){
                e.printStackTrace();
            }

            TeacherService teacherService = new TeacherServiceImpl();
            Teacher loginTeacher = teacherService.findTeacherByUsername(teacher);

            if (loginTeacher != null){
                if (loginTeacher.getPassword() != null){
                    request.setAttribute("register_msg","该老师已经注册，请直接登录!");
                    request.getRequestDispatcher("/register.jsp").forward(request,response);
                    return;
                }
                int count = teacherService.register(teacher);
                if (count >0){
                    request.setAttribute("register_msg","注册成功!");
                    request.getRequestDispatcher("/register.jsp").forward(request,response);
//                    //跳转登录
//                    response.sendRedirect(request.getContextPath()+"/index.jsp");
                }else{
                    request.setAttribute("register_msg","注册失败!");
                    request.getRequestDispatcher("/register.jsp").forward(request,response);
                }

            }else{
                request.setAttribute("register_msg","查无此老师，无法注册!");
                request.getRequestDispatcher("/register.jsp").forward(request,response);
//            response.sendRedirect(request.getContextPath()+"/index.jsp");
            }
        }else if(map.get("loginType")[0].equals("student")){
//            System.out.println(1);
            Student student = new Student();
            try{
                BeanUtils.populate(student,map);
                System.out.println(student);
            }catch (IllegalAccessException e){
                e.printStackTrace();
            }catch (InvocationTargetException e){
                e.printStackTrace();
            }
            StudentService studentService = new StudentServiceImpl();
            Student loginStudent = studentService.findStudentByUsername(student);

            if (loginStudent != null){
                if (loginStudent.getPassword() != null){
                    request.setAttribute("register_msg","此学生已经注册，请直接登录!");
                    request.getRequestDispatcher("/register.jsp").forward(request,response);
                    return;
                }else{
                    int count = studentService.register(loginStudent);
                    if (count > 0){
                        request.setAttribute("register_msg","注册成功!");
                        request.getRequestDispatcher("/register.jsp").forward(request,response);
                    }else{
                        request.setAttribute("register_msg","注册失败!");
                        request.getRequestDispatcher("/register.jsp").forward(request,response);
                    }

                }
//                request.getSession().setAttribute("user",loginStudent);
                //跳转主页面
//                response.sendRedirect(request.getContextPath()+"/main");
            } else{
                request.setAttribute("register_msg","查无此学生，无法注册!");
                request.getRequestDispatcher("/register.jsp").forward(request,response);
//            response.sendRedirect(request.getContextPath()+"/index.jsp");
            }
        }

    }
}
